package com.jianyun.jyzs.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.jianyun.jyzs.ThisApp;
import com.jianyun.jyzs.bean.AnnouncementComment;
import com.jianyun.jyzs.bean.AnnouncementList;
import com.jianyun.jyzs.bean.BadgeBean;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmFunnelList;
import com.jianyun.jyzs.bean.CrmSignDB;
import com.jianyun.jyzs.bean.EnNatureResponse;
import com.jianyun.jyzs.bean.EngProjectLocResult;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.bean.EngineerTypeResult;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.bean.EnterInfo;
import com.jianyun.jyzs.bean.IndustryResponse;
import com.jianyun.jyzs.bean.IssueTypeResponse;
import com.jianyun.jyzs.bean.MessageData;
import com.jianyun.jyzs.bean.OrderResponse_;
import com.jianyun.jyzs.bean.QualityRespon;
import com.jianyun.jyzs.bean.SafeInsResponse;
import com.jianyun.jyzs.bean.SafeIssueType;
import com.jianyun.jyzs.bean.Sign;
import com.jianyun.jyzs.bean.StarStaff;
import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.bean.WoProject;
import com.jianyun.jyzs.bean.WoWorkType;
import com.jianyun.jyzs.bean.WorkOrderDetialResponse;
import com.jianyun.jyzs.dao.ItemModel;
import com.jianyun.jyzs.dao.ModelDao;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liml on 17/5/16.
 */

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static final String TABLE_NAME = "jyzs.db";
    // 数据库version
    private static final int DATABASE_VERSION = 2;

    private Map<String, Dao> daos = new HashMap<String, Dao>();

    private static DatabaseHelper instance;

    public static synchronized DatabaseHelper getHelper(Context context){
        try{
            context = context.getApplicationContext();
            if (instance==null){
                synchronized (DatabaseHelper.class){
                    if (instance==null){
                        instance = new DatabaseHelper(context);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return instance;
    }

    public DatabaseHelper(Context context) {
        super(context, TABLE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {

            TableUtils.createTableIfNotExists(connectionSource, EnterInfo.class);
            TableUtils.createTableIfNotExists(connectionSource, Sign.class);
            TableUtils.createTableIfNotExists(connectionSource, EngineeringResult.EngineerBean.class);
            TableUtils.createTableIfNotExists(connectionSource, OrderResponse_.PoBean.class);
            TableUtils.createTableIfNotExists(connectionSource, EngineerProjectResult.EngProject.class);
            TableUtils.createTableIfNotExists(connectionSource, EngineerTypeResult.EngineerType.class);
            TableUtils.createTableIfNotExists(connectionSource, EngProjectLocResult.EngTypeLocBean.class);
            TableUtils.createTableIfNotExists(connectionSource, AnnouncementList.Announcement.class);
            TableUtils.createTableIfNotExists(connectionSource, AnnouncementComment.AnnComment.class);
            TableUtils.createTableIfNotExists(connectionSource, EnterInfo.class);
            TableUtils.createTableIfNotExists(connectionSource, OrderResponse_.PoBean.class);
            TableUtils.createTableIfNotExists(connectionSource, EnNatureResponse.EnNatureBean.class);
            TableUtils.createTableIfNotExists(connectionSource, IndustryResponse.IndustryBean.class);
            TableUtils.createTableIfNotExists(connectionSource, CrmDetial.CrmDetailBean.class);
            TableUtils.createTableIfNotExists(connectionSource, CrmFunnelList.FunnelBean.class);
            TableUtils.createTableIfNotExists(connectionSource, CrmSignDB.class);
            TableUtils.createTableIfNotExists(connectionSource, WorkOrderDetialResponse.WorkOrder.class);
            TableUtils.createTableIfNotExists(connectionSource, WoProject.Project.class);
            TableUtils.createTableIfNotExists(connectionSource, WoWorkType.WoType.class);
            TableUtils.createTableIfNotExists(connectionSource, QualityRespon.QualityRes.class);
            TableUtils.createTableIfNotExists(connectionSource, IssueTypeResponse.Issuety.class);
            TableUtils.createTableIfNotExists(connectionSource, SafeInsResponse.SafeBean.class);
            TableUtils.createTableIfNotExists(connectionSource, StarStaff.class);
            TableUtils.createTableIfNotExists(connectionSource, SafeIssueType.SafeIssue.class);
            TableUtils.createTableIfNotExists(connectionSource, ItemModel.class);
            TableUtils.createTableIfNotExists(connectionSource, User.class);
            TableUtils.createTableIfNotExists(connectionSource, MessageData.class);
            TableUtils.createTableIfNotExists(connectionSource, BadgeBean.class);
            //TableUtils.createTableIfNotExists(connectionSource, SysConfig.class);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        //
        try {
            new ModelDao(ThisApp.applicationContext).clear();
            TableUtils.dropTable(connectionSource,CrmDetial.CrmDetailBean.class,true);
            TableUtils.dropTable(connectionSource, EngineeringResult.EngineerBean.class,true);
            TableUtils.dropTable(connectionSource,User.class,true);
            onCreate(sqLiteDatabase,connectionSource);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    public synchronized Dao getDao(Class clazz) throws SQLException{
        Dao dao = null;
        String className = clazz.getSimpleName();

        if (daos.containsKey(className)){
            dao = daos.get(className);
        }
        if (dao == null){
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }
    /**
     * 释放资源
     */
    @Override
    public void close(){
        super.close();
        if(daos != null){
            daos.clear();
        }
    }
}
